package com.xyzboom.database.utils;

import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseUtils {
    public static final String url = "jdbc:mysql://127.0.0.1/920106840226_jdbc?useUnicode=true&characterEncoding=utf-8";
    public static final String username = "user_920106840226";
    public static final String password = "000000";
    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final BasicDataSource basicDataSource;

    static {
        basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName(JDBC_DRIVER);
        basicDataSource.setUsername(username);
        basicDataSource.setPassword(password);
        basicDataSource.setUrl(url);
        DbUtils.loadDriver(JDBC_DRIVER);
    }

    public static DataSource getDataSource() {
        return basicDataSource;
    }
    public static QueryRunner getQueryRunner() {
        return new QueryRunner(basicDataSource);
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, username, password);
    }

    public String getUrl() {
        return url;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }
}
